<?php

namespace app\api\model;

use think\Model;

class Product extends Base
{
    protected $table = 'product';
    protected $autoWriteTimestamp = 'datetime';

    public function imgs(){

        return $this->hasOne('ProductImage','product_id','id');
    }

    public function property(){

        return $this->hasOne('ProductProperty','product_id','id');
    }

    public function getMainImgUrlAttr($value)
    {
        return config('setting.img_prefix').$value;
    }


    public function getNewProducts()
    {
        try{
            $products = $this->limit(8)
                ->order('create_time desc')
                ->select();
        }catch (\Exception $e){

            $products = null;
        }
        return $products;
    }

    public function getDetailProduct($id){
        try{

            $product = $this->with(['imgs','property'])->find($id);

        }catch (\Exception $e){

            $product = null;
        }
        return $product;

    }
}